package com.gxuwu.admin.controller;

import com.gxuwu.admin.entity.Permission;
import com.gxuwu.admin.service.PermissionService;
import com.gxuwu.admin.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.type.SimpleTypeRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.CredentialException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "菜单权限管理")
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;



    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public R doAssign(String roleId,String[] permissionIdList) {

        permissionService.doAssign(roleId, permissionIdList);

        return R.ok();
    }



    /**
     * 根据当前角色查询所有权限
     * @return
     */
    @ApiOperation(value = "根据角色获取菜单")
    @PostMapping("/toAssign")
    public R toAssign(@RequestBody String roleId){

        Map<String, List<Permission>> permissionMap = permissionService.toAssign(roleId);


        return R.ok().data("permissionList",permissionMap);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有菜单")
    public R getPermission(){

        List<Permission> allPermission = permissionService.getAllPermission();


        return R.ok().data("chidren",allPermission);


    }



}
